﻿CREATE PROCEDURE [dbo].[proc_Customer_Auditlog_GetList]
	(
	@username nvarchar(50),
	@StartDate nvarchar(50),
	@EndDate nvarchar(50),
	@SText nvarchar(50),
	@StartIndex int,
	@EndIndex int
	)

AS
Begin
	Declare @sRq Datetime,@eRq Datetime
	Set @sRq = Convert(Datetime,@StartDate+' 00:00:00')
	Set @eRq = Convert(Datetime,@EndDate+' 23:59:59')

	;WITH list As(Select ROW_NUMBER() OVER (ORDER BY AuditDate Desc,Id Desc) AS Row,
		Id,
		CustomerId,
		IsNull(CustomerName,'') As CustomerName,
		Description,
		(Select ename From employee where employee.username=audituser) as AuditUser,
		AuditDate,
		SalesName
--			(select title from customer_protect where customer_protect.id=type) as type
	From Customer_AuditLog 
	Where (SalesName=@UserName Or AuditUser=@UserName)
		And AuditDate>=@sRq And AuditDate<=@eRq
		And (@SText='' Or CustomerName like '%'+@SText+'%')
	 )

	Select *,
	IsNull((Select Top 1 EName From Employee Where UserName=list.SalesName),'') As [type],
	(Select Count(0) From list) As RecordCount
	From list Where Row between @StartIndex and @EndIndex Order By Row
End
